package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class NetfreeRoamingHistoryAdapter extends BaseListAdapter<LinkedTreeMap> {

    public NetfreeRoamingHistoryAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_netfree_roaming_item, parent, false);
                holder.outName = (TextView) convertView.findViewById(R.id.outName);
                holder.money = (TextView) convertView.findViewById(R.id.money);
                holder.status = (TextView) convertView.findViewById(R.id.status);
                holder.inName = (TextView) convertView.findViewById(R.id.inName);
                holder.remrak = (TextView) convertView.findViewById(R.id.remrak);
                holder.myfs = (TextView) convertView.findViewById(R.id.myfs);
                holder.time = (TextView) convertView.findViewById(R.id.time);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.outName.setText(mDataList.get(position).get("out_branch_name")+"");
            holder.inName.setText(mDataList.get(position).get("in_branch_name")+"");
            if (mDataList.get(position).containsKey("out_remark")){
                holder.remrak.setText("描述："+mDataList.get(position).get("out_remark")+"");
            }else{
                holder.remrak.setText("暂无描述");
            }
            holder.money.setText("￥"+decimalFormat.format(mDataList.get(position).get("transfer_amount"))+"");
            if (decimalFormat.format(mDataList.get(position).get("transfer_status")).equals("0")){
                holder.status.setText("漫游失败");
            }else if (decimalFormat.format(mDataList.get(position).get("transfer_status")).equals("1")){
                holder.status.setText("漫游成功");
            }else{
                holder.status.setText("漫游成功");
            }
            holder.time.setText(stampToDate(mDataList.get(position).get("transfer_date")+""));

            if (decimalFormat.format(mDataList.get(position).get("union_type")).equals("0")){
                holder.myfs.setText("本金漫游");
            }else if(decimalFormat.format(mDataList.get(position).get("union_type")).equals("1")){
                holder.myfs.setText("余额漫游");
            }else if(decimalFormat.format(mDataList.get(position).get("union_type")).equals("9")){
                holder.myfs.setText("当前门店没有参与漫游联盟");
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView outName, inName, time, money, status, myfs, remrak;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    public static String stampToDate(String s){
        s = s.replace("/", "").replace("Date(","").replace(")", "");
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
}
